import pandas as pd
from pathlib import Path
import json

def process_data(filename):

    cols = ['Input','Jurisdiction','TaxType','Change','EffectiveDate']

    """
    json format : key : { 'answers' : string [], 
                            context : string, 
                            'question' : string , 
                            'spans' = list of list[[i,j]...] 
                        }
    """


    cols_to_add = ['answers','question','spans','uuid']
    path = Path(filename)
    df = pd.read_excel(path,usecols=cols,).fillna(value = "")
    # print(df)

    question_dict = {
        "Input": ["context"],
        "Jurisdiction": ["Where did the change happen?","what location did the tax change?"],
        "TaxType": ["What were the items or taxes that changed?","what tax is being talked about?"],
        "Change": ["What were the changes?","What is the initial and final value for the tax change?"],
        "EffectiveDate": ["What is the start or effective date for the change?",
                          "what is the start or end date?"]

    }
    questions = []
    for k,vl in question_dict.iteritems():
        # if k != 'Input':
            for v in vl:
                df[v] = df[k]
                if k != 'Input':
                    questions.append(v)

    # df = df.rename(columns=question_dict)

    # questions = [value for value in question_dict.values() if value != "context"]
    # questions = [

    _json_ = []

    """
    paragraph : [
    {
        "context : ""
        "qas" : [
        {
            "answers : [
                    {
                    "answer_start : "",
                    "text" : ""
                    }
                ],
                "id" : "",
                "is_impossible" : false,
                "question" : ""
        }]
        
    }]
    """
    for i in df.index:
        context = df.iloc[i]['context'].lower()
        qas = []
        for q in questions:
            text = unicode(df.iloc[i][q]).lower()
            index = context.find(text)
            d = {
                "answers" : [
                    { "answer_start" : index,
                      "text": text
                    }
                ] if len(text) > 0 and index > -1 else [],
                "id" : "",
                "is_impossible" : False,
                "question": q
                # "context" : context,

                # "spans" : token_span(context)
            }
            qas.append(d)
        yield {
            "context" : context,
            "qas" : qas
        }

def token_span(sentence):
    tokens = sentence.split()
    spans = []
    for t in tokens:
        spans.append((t, sentence.find(t), sentence.find(t) + len(t)))
    return spans

if __name__ == '__main__':
    file_name = "/Users/avinash.v/Desktop/RateAnnotations.xlsx"

    # todo : new file with 1000 qa
    file_name = "/Users/avinash.v/Desktop/QA_NW_Output.xlsx"
    test_json_output_file = "/sample_output_new.json"
    p = Path("/Users/avinash.v/Desktop" + test_json_output_file)
    json_list = (process_data(file_name))
    tuple_list = []
    count = 0
    paragraphs = []
    for d in json_list:
        paragraphs.append(d)
        # tuple_list.append((count,d))
        # print(d)
        # count += 1

    print(count)
    with open(str(p), 'w') as outfile:
        # json.dump(dict(tuple_list),outfile)
        json.dump({"paragraphs" : paragraphs,
                   "title" : "Tax"
                   }, outfile)

    # print(json_list)
    # print(len(json_list))

